var dir = "";
$(function() {
	
	var scripts = document.getElementsByTagName('script');
	if(scripts.length){
	    // 获取最近一个加载的script，即这个js本身
	    var lastScript = scripts[scripts.length-1];
	    // 获取此js的路径  
	    //var scriptSrc = lastScript.src;
	    var scriptSrc = lastScript.src;
	    dir=getQueryString("dir",scriptSrc);
	}
	
	$("head  title").text(config.name);
	
	
	 if(!isFast()){
         document.getElementById("tip").removeAttribute("style");
         document.getElementById("tipmask").removeAttribute("style");
       }
       
	    $(document).keydown(function (event) {
	    	
	        if (event.keyCode == 13) {
	        	app.login();
	        }
	      });
	    
	   
	
});

function isFast(){
    var explorer = window.navigator.userAgent.toLowerCase();
    if (window.navigator.userAgent.indexOf('compatible') !== -1) {
        return false
    }
    if(window.navigator.userAgent.indexOf('AppleWebKit') !== -1) {
        return true
    }else{
       return false
    }  
  }


var app = new Vue({
	el: '#app',
	
	data: {
		title: "",
		name:"",
		password:"",
		code:"",
		error:"",
	},
	mounted: function(){
		this.initData();
	},	
	
	methods: {
		initData: function(){
			var that = this;
			that.title=config.name;
			this.refreshimg();
		},
		
		 refreshimg:function() { 
		
				tools.ajaxRequestAsync("pub/login/checkCode", {}, function(data){
					   $("#randomImage").attr("src", "data:image/png;base64,"+data.image_base64);
			            $("#randomImage").attr("chkcode",data.chkcode);	
				});
		},
	
		
		 login:function() { 
			
		     var that=this;
			 if(this.name==""){  
		         // this.showError("请输入登录名");
				  this.error="请输入登录名";
		          return false;
		     }
			 if(this.password==""){  
		         // this.showError("请输入登录密码");
		          this.error="请输入登录密码";
		          return false ;
		     }
			 if(this.code==""){  
		         // this.showError("请输入验证码");
				  this.error="请输入验证码";
		          return false;
		     }
			 
			 var chkcode= $("#randomImage").attr("chkcode");
		    	if(this.code!=chkcode){
		    		 //this.showError("验证码不正确");
		    		 this.error="验证码不正确";
		    		return false;
		    	}		    	
		    	
		   
		    	var pwd= this.password.MD5();
		    	var postData = {
		      			
		      			"name": this.name,
		      			"pwd": pwd,
		      			"code": this.code,
		      		};
		      		
		      		tools.ajaxRequestAsyncEx("pub/login/loginCheck",postData, function(data){
		    			if(data.respCode=="0"){
		    				that.$message({
		                        showClose: true,
		                        type: 'success',
		                        message: '登录成功!'
		                      });  
		    				 location.href= dir+"/mainindex.jsp?r="+Math.random();
		    				 return false;
		      			}else{	  
		      			
		      			  that.error=data.respDesc;		      			  
				          return false;
		      			}
		      		});
		    	
		},
		showError:function(info){
			var i = info==undefined?"初始化数据失败":info;
			this.$alert(i, '系统错误', {
		          confirmButtonText: '确定'
		    });			
		}
		
	}
	
});